Home:ALL Converter>Bash: Close MySQL connection when server fails

Bash: Close MySQL connection when server fails

Ask Time:2017-04-30T23:46:27         Author:andrew

Json Formatter

I have a simple script to connect to a extern MySQL server.

#!/bin/bash
server=1.2.3.1
port=3306
user=root
pass=root
db=radius

output=$(mysql -N -h $server -u $user -p$pass $db -e  "select * from table)
echo "$output"

The problem is when the mysql service fails on server 1.2.3.1, because the script gets stucked waiting for answer a long time!

How can I wait only for 1 second and if server doesn't respond, the script kills the process? Or how can I check if the service is up on the remote server before?

Regards.

Author:andrew,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/43708222/bash-close-mysql-connection-when-server-fails
yy